import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
} from '@nestjs/swagger';
import { ConfigCenterService } from './config-center.service';
import { DynamicConfigService } from './dynamic-config.service';

/**
 * 配置中心控制器
 * 对应 Java: ConfigController
 */
@ApiTags('配置中心')
@Controller('config')
export class ConfigCenterController {
  constructor(
    private readonly configCenter: ConfigCenterService,
    private readonly dynamicConfig: DynamicConfigService,
  ) {}

  @Get('value/:key')
  @ApiOperation({ summary: '获取配置值' })
  @ApiParam({ name: 'key', description: '配置键' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiQuery({ name: 'defaultValue', required: false, description: '默认值' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getConfigValue(
    @Param('key') key: string,
    @Query('siteId') siteId: number = 0,
    @Query('defaultValue') defaultValue?: string,
  ) {
    const value = await this.configCenter.getConfigValue(
      key,
      defaultValue,
      siteId,
    );
    return {
      success: true,
      data: { key, value, siteId },
    };
  }

  @Post('value')
  @ApiOperation({ summary: '设置配置值' })
  @ApiResponse({ status: 200, description: '设置成功' })
  @HttpCode(HttpStatus.OK)
  async setConfigValue(
    @Body() body: { key: string; value: any; siteId?: number; addon?: string },
  ) {
    const { key, value, siteId = 0, addon = '' } = body;

    await this.configCenter.setConfigValue(key, value, siteId, addon);

    return {
      success: true,
      message: '配置设置成功',
      data: { key, value, siteId },
    };
  }

  @Delete('value/:key')
  @ApiOperation({ summary: '删除配置值' })
  @ApiParam({ name: 'key', description: '配置键' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiResponse({ status: 200, description: '删除成功' })
  async deleteConfigValue(
    @Param('key') key: string,
    @Query('siteId') siteId: number = 0,
  ) {
    await this.configCenter.deleteConfigValue(key, siteId);

    return {
      success: true,
      message: '配置删除成功',
      data: { key, siteId },
    };
  }

  @Post('refresh')
  @ApiOperation({ summary: '刷新配置缓存' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiQuery({ name: 'key', required: false, description: '配置键' })
  @ApiResponse({ status: 200, description: '刷新成功' })
  async refreshConfigCache(
    @Query('siteId') siteId: number = 0,
    @Query('key') key?: string,
  ) {
    await this.configCenter.refreshConfigCache(siteId, key);

    return {
      success: true,
      message: '配置缓存刷新成功',
      data: { siteId, key },
    };
  }

  @Post('push')
  @ApiOperation({ summary: '推送配置变更' })
  @ApiResponse({ status: 200, description: '推送成功' })
  async pushConfigChange(
    @Body() body: { siteId: number; key: string; value: any },
  ) {
    const { siteId, key, value } = body;

    await this.configCenter.pushConfigChange(siteId, key, value);

    return {
      success: true,
      message: '配置推送成功',
      data: { siteId, key, value },
    };
  }

  @Post('hot-reload')
  @ApiOperation({ summary: '热重载配置' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiQuery({ name: 'key', required: false, description: '配置键' })
  @ApiResponse({ status: 200, description: '热重载成功' })
  async hotReloadConfig(
    @Query('siteId') siteId: number = 0,
    @Query('key') key?: string,
  ) {
    await this.configCenter.hotReloadConfig(siteId, key);

    return {
      success: true,
      message: '配置热重载成功',
      data: { siteId, key },
    };
  }

  @Get('version/:key')
  @ApiOperation({ summary: '获取配置版本' })
  @ApiParam({ name: 'key', description: '配置键' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getConfigVersion(
    @Param('key') key: string,
    @Query('siteId') siteId: number = 0,
  ) {
    const version = await this.configCenter.getConfigVersion(siteId, key);

    return {
      success: true,
      data: { key, version, siteId },
    };
  }

  @Post('rollback')
  @ApiOperation({ summary: '回滚配置' })
  @ApiResponse({ status: 200, description: '回滚成功' })
  async rollbackConfig(
    @Body() body: { siteId: number; key: string; version: number },
  ) {
    const { siteId, key, version } = body;

    await this.configCenter.rollbackConfig(siteId, key, version);

    return {
      success: true,
      message: '配置回滚成功',
      data: { siteId, key, version },
    };
  }

  @Get('group/:group')
  @ApiOperation({ summary: '获取配置分组' })
  @ApiParam({ name: 'group', description: '配置分组' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getConfigGroup(
    @Param('group') group: string,
    @Query('siteId') siteId: number = 0,
  ) {
    const configs = await this.dynamicConfig.getConfigGroup(siteId, group);

    return {
      success: true,
      data: { group, configs, siteId },
    };
  }

  @Get('history/:key')
  @ApiOperation({ summary: '获取配置历史' })
  @ApiParam({ name: 'key', description: '配置键' })
  @ApiQuery({
    name: 'siteId',
    required: false,
    description: '站点ID',
    example: 0,
  })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getConfigHistory(
    @Param('key') key: string,
    @Query('siteId') siteId: number = 0,
  ) {
    const history = await this.dynamicConfig.getConfigHistory(siteId, key);

    return {
      success: true,
      data: { key, history, siteId },
    };
  }

  @Get('status')
  @ApiOperation({ summary: '获取配置中心状态' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getConfigCenterStatus() {
    const status = this.configCenter.getConfigCenterStatus();

    return {
      success: true,
      data: status,
    };
  }
}
